package com.dong.component;
import com.dong.beans.Zone1AlarmResult;
import com.dong.mybatis.entity.Zone1;
import com.dong.mybatis.entity.Zone1AlarmRecord;
import com.dong.mybatis.entity.Zone2AlarmRecord;
import com.dong.mybatis.entity.Zone3AlarmRecord;
import com.dong.mybatis.mapper.Zone1AlarmRecordMapper;
import com.dong.mybatis.mapper.Zone2AlarmRecordMapper;
import com.dong.mybatis.mapper.Zone3AlarmRecordMapper;
import com.dong.service.Zone1AlarmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Component
public class AlarmHandler {

    // 注入服务和Mapper
    @Autowired
    private Zone1AlarmService zone1AlarmService;

    @Autowired
    private Zone1AlarmRecordMapper zone1AlarmRecordMapper;
    @Autowired
    private Zone2AlarmRecordMapper zone2AlarmRecordMapper;
    @Autowired
    private Zone3AlarmRecordMapper zone3AlarmRecordMapper;

    /**
     * 处理Zone1实时数据并保存报警记录
     * @param zone1 从数据库查询到的Zone1实时数据对象
     */
    public void handleZone1Data(Zone1 zone1,String zoneId) {
        if (zone1 == null) {
            System.out.println("Zone1数据为空，不处理");
            return;
        }

        // 1. 收集需要判断的参数（参数名与阈值表保持一致）
        Map<String, Double> paramValues = new HashMap<>();
        paramValues.put("temp", zone1.getTemp());  // 温度参数
        paramValues.put("humi", zone1.getHumi());  // 湿度参数
        paramValues.put("a", zone1.getA());
        paramValues.put("v", zone1.getV());
        paramValues.put("l", zone1.getL());        // 电压参数
        // 可根据实际需求添加更多参数（如A、L等）

        // 2. 调用服务批量判断阈值
        List<Zone1AlarmResult> alarmResults = zone1AlarmService.batchCheckThresholdAlarm(paramValues);
       for (Zone1AlarmResult alarmResult : alarmResults) {
           log.info("报警结果💕：{}", alarmResult);
           System.out.println(alarmResult);
       }
        // 3. 筛选出触发报警的结果
        List<Zone1AlarmResult> alarmList = alarmResults.stream()
                .filter(Zone1AlarmResult::getIsAlarm)  // 只保留报警项
                .collect(Collectors.toList());

        // 4. 保存报警记录到数据库
        if (!alarmList.isEmpty()) {
            System.out.println("检测到" + alarmList.size() + "个报警，开始保存记录...");

            for (Zone1AlarmResult alarm : alarmList) {
                // 创建报警记录实体
                Zone1AlarmRecord record = new Zone1AlarmRecord();
                record.setParameter(alarm.getParameter());       // 报警参数名
                record.setActualValue(alarm.getActualValue());   // 实际数值
                record.setThresholdMin(alarm.getMinThreshold()); // 最低阈值
                record.setThresholdMax(alarm.getMaxThreshold()); // 最高阈值
                record.setAlarmDesc(alarm.getAlarmMsg());        // 报警描述
                // alarmTime由数据库自动生成，无需手动设置

                // 插入数据库
                int rows = 0;
                switch (zoneId) {
                    case "zone1":
//                        zone1AlarmRecordMapper.insert(record);
                         rows = zone1AlarmRecordMapper.insert(record);
                        log.info("zone1报警记录保存成功：{}", record.toString());
                        break;
                    case "zone2":
                        Zone2AlarmRecord  zone2AlarmRecord = new Zone2AlarmRecord();
                        BeanUtils.copyProperties(record, zone2AlarmRecord);
                        log.info("zone2报警记录保存成功：{}", zone2AlarmRecord.toString());
                        rows = zone2AlarmRecordMapper.insert(zone2AlarmRecord);
                        System.out.println("zone2报警记录保存成功：" + alarm.getAlarmMsg());
                        break;
                    case "zone3":
                        Zone3AlarmRecord zone3AlarmRecord = new Zone3AlarmRecord();
                        BeanUtils.copyProperties(record, zone3AlarmRecord);

                        log.info("zone3报警记录保存成功：{}", zone3AlarmRecord.toString());
                        rows = zone3AlarmRecordMapper.insert(zone3AlarmRecord);
                        System.out.println("zone3报警记录保存成功：" + alarm.getAlarmMsg());
                        break;
                    default:
                        System.out.println("报警记录保存成功：" + alarm.getAlarmMsg());
                        break;
                }
                if (rows > 0) {
                    System.out.println("报警记录保存成功：" + alarm.getAlarmMsg());
                } else {
                    System.out.println("报警记录保存失败：" + alarm.getAlarmMsg());
                }
            }
        } else {
            System.out.println("未检测到报警");
        }
    }
}
